package kcsj;

import sxx.MyUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Scanner;

public class ProgramStorage {
    private final int n;//程序的个数
    private final int[] l;//存放第i个程序的长度
    private final int[] p;//存放第i个陈孤村读取的次数

    public ProgramStorage(int index) {
        InputStream fin = ProgramStorage.class.getResourceAsStream("/data/ProgramStorage/test/tape" + index + ".in");
        assert fin != null : "找不到测试数据!";
        Scanner scanner = new Scanner(fin);
        n = scanner.nextInt();
        l = new int[n];
        p = new int[n];
        for (int i = 0; i < n; i++) {
            l[i] = scanner.nextInt();//程序的长度
            p[i] = scanner.nextInt();//程序读取的次数
        }
        scanner.close();
        try {
            fin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public float solve() {
        float sum = MyUtil.sumArray(p);
        float[] t = new float[n];//存放l与p的乘积
        float sum_t = 0;//平均读取时间

        for (int i = 0; i < n; i++) {
            t[i] = l[i] * p[i] / sum;
        }

        Arrays.sort(t);//非递减排序,有限选择程序长度与出现概率之积最小的放在前面

        for (int i = 0; i < n; i++) {
            sum_t += (n - i) * t[i];//求和
        }
        return sum_t;
    }

    public static void main(String[] args) {
        System.out.println("信计1902张倚睿");
        for (int i = 0; i <= 10; i++) {
            ProgramStorage programStorage = new ProgramStorage(i);
            System.out.println(programStorage.solve());
        }
    }
}
